package com.example.smart_city_0829.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.entity.user.Login;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class LoginActivity extends BaseActivity {

    private EditText userName;
    private EditText password;
    private TextView register;
    private Button login;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        userName = (EditText) findViewById(R.id.user_name);
        password = (EditText) findViewById(R.id.password);
        register = (TextView) findViewById(R.id.register);
        login = (Button) findViewById(R.id.login);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                migrateTo(RegisterActivity.class);
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userName = LoginActivity.this.userName.getText().toString();
                final String password = LoginActivity.this.password.getText().toString();
                final HashMap<String, Object> params = new HashMap<>();
                params.put("username", userName);
                params.put("password", password);
                api.login(RequestBody.create(gson.toJson(params), MediaType.parse("application/json"))).enqueue(new NetCallBack<Login>() {
                    @Override
                    public void onResponse(Call<Login> call, Response<Login> response) {
                        final Login body = response.body();
                        showToastShort(body.getMsg());
                        if (body.getMsg().contains("成功")) {
                            final String token = body.getToken();
                            editor.putString("token", token)
                                    .putString("password", password)
                                    .apply();
                            Log.e("userToken", token);
                            LoginActivity.this.finish();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "登录";
    }
}